1. /* sdfddiv2.cpp by K.Tsuru */
  2. // function ID = 327 DRADIX, BRADIX
  3. /*********************************************
  4. SDouble and SDecimal classes
  5. n/2
  6. It is faster than DsDiv but without large roop
  7. use DsDiv(n, 2).
  8. **********************************************/
  9. #ifndef SN_H
  10. #include "sn.h"
  11. #endif
  12. SDouble DDiv2(const SDouble& n){
  13. uint rf = n.First(), rl = n.Last(); // rl < n.CurrentMaxSize()
  14. if(n.IsPointFixed() && n.IsHidden() && (rf == rl) && (n.figure(rf) < 2) ){
  15. return SDZero(n); // |n| == 0.0000 ..... 0001
  16. }
  17. SDouble result(n.Type(), 0);
  18. result.rdxExp = n.rdxExp;
  19. const fType* np;
  20. if( rf && (n.figure(rf) == 1) && !n.IsPointFixed()){
  21. //If first figure is equal to one,it shifts to upper to avoid to lose precision.
  22. result = n;
  23. result.ShiftArray(-1); result.rdxExp--;
  24. rf--; rl--;
  25. np = result.figure.Elements();
  26. }else {
  27. np = n.ReadFigures();
  28. //do not use result.figure.size(n.figure.size(), -1);
  29. result.valloc(n.figure.size(), -1);
  30. if(rf) result.figure.clear(0, rf-1);
  31. result.figure.clear(rl+1);
  32. }
  33. fType* rp = result.figure.Elements();
  34. fType rem = 0, u; // rem : remainder
  35. fType rdx = n.Radix();
  36. uint i;
  37. #ifndef NDEBUG
  38. result.figure(rl); n.figure(rl);
  39. #endif
  40. for(i = rf; i <= rl; i++) {
  41. u = np[i];
  42. if( rem ) u += rdx;
  43. rem = u & 1; // rem = 0 or 1
  44. rp[i] = u >> 1;
  45. }
  46. // rem = 0 or 1
  47. if(rem && result.Reserve(i)){
  48. rp = result.figure.Elements();
  49. rl = i;
  50. result.figure[i] = rdx/2;//It increases by one figure.
  51. }
  52. /******************************************************
  53. Do not set result.aHead = rl.
  54. Dividing n = 0.1234 0000 0000 ... 0000 0001 by two gives
  55. result = 0.0617 0000 0000 ... 0000.
  56. The last position largely moves to upper.
  57. *******************************************************/
  58. while(!rp[rl] && (rl > rf) ) rl--;
  59. if( (rl == rf) && !rp[rl] ){
  60. result.SetZero(); return result;
  61. }
  62. result.aHead = rl;
  63. while(!rp[rf]) rf++;
  64. result.aTail = rf;
  65. result.SetSign(n.Sign(327));
  66. result.Reform(327);
  67. return result;
  68. }

sdfddiv2.cpp : last modifiled at 2017/03/13 14:31:58(2,057 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).